﻿using GameCommon.ComponentInterfaces;

using GameGraphics.Components;

using Microsoft.Xna.Framework;

namespace GameGraphics.Screens.GameBoard
{
    internal class TileBuildAnimation : IAnimation
    {
        private const int Step = 4;
        
        private readonly BoardTile boardTile;

        private readonly int initialY;

        private double elapsedTime;
        
        public TileBuildAnimation(BoardTile boardTile, int offsetY)
        {
            this.initialY = boardTile.Position.Y;
            this.boardTile = boardTile;
            this.boardTile.Position.Y -= offsetY;
        }

        public bool IsFinished { get; private set; }

        public void Update(GameTime gameTime)
        {
            this.elapsedTime += gameTime.ElapsedGameTime.TotalSeconds;
            this.FadeIn();
        }

        private void FadeIn()
        {
            var newY = this.boardTile.Position.Y + Step;

            if (newY >= initialY)
            {
                this.boardTile.Position.Y = initialY;
                this.IsFinished = true;
                return;
            }

            this.boardTile.Position.Y += Step;
        }
    }
}